#include <psp2common/types.h>
#include <psp2kern/kernel/sysclib.h>
#include <psp2kern/display.h>

#include "floating.h"
#include "scr_print.h"
#include "utils.h"
#include "gui.h"
#include "config.h"
#include "main.h"

#define INFOS_PADDING 0u
#define INFOS_ITEM_MARGIN_T 1u
#define INFOS_ITEM_HEIGHT (fontGetFontSize())

#define CPU_INFO_HEIGHT INFOS_ITEM_HEIGHT
#define MEM_INFO_HEIGHT (INFOS_ITEM_HEIGHT * 3 + INFOS_ITEM_MARGIN_T * 2)

#define INFOS_HEIGHT (CPU_INFO_HEIGHT + MEM_INFO_HEIGHT + INFOS_ITEM_MARGIN_T)
#define INFOS_WIDTH 200u
#define INFOS_SX 0u
#define INFOS_SY 0u
#define INFOS_DX (INFOS_SX + INFOS_WIDTH)
#define INFOS_DY (INFOS_SY + INFOS_HEIGHT)
#define CPU_INFO_WIDTH INFOS_WIDTH
#define MEM_INFO_WIDTH INFOS_WIDTH

#define MISC_CHILD_MARGIN_L 8u
#define MISC_HEIGHT (fontGetFontSize())
#define MISC_SX (scrPrintGetScreenWidth())
#define MISC_SY 0u
#define MISC_DX MISC_SX
#define MISC_DY (MISC_SY + MISC_HEIGHT)

#define COLOR_WINDOW_BG 0xFF7A4423
#define COLOR_TEXT_FG_DEF WHITE
#define COLOR_TEXT_BG_DEF 0u
#define COLOR_CPU_NAME_FG 0xFF33CC00
#define COLOR_CPU_VALUE_FG ORANGE
#define COLOR_MEM_NAME_FG 0xFFCD96CD
#define COLOR_MEM_VALUE_FG 0xFFFFC1C1

void drawFloating()
{
    char str_buf[64];
    int x, y;
    int w;

    /* 设置文字颜色 */
    scrPrintSetBackColor(COLOR_TEXT_BG_DEF);
    scrPrintSetTextColor(COLOR_TEXT_FG_DEF);

    x = INFOS_SX;
    y = INFOS_SY;

    /* CPU信息 */
    if (g_setting_config.show_cpu)
    {
        scrPrintRectangle(x, y, CPU_INFO_WIDTH, CPU_INFO_HEIGHT, COLOR_WINDOW_BG);

        scrPrintSetTextColor(COLOR_CPU_NAME_FG);
        scrPrintText(x, y, "CPU:");
        scrPrintSetTextColor(COLOR_CPU_VALUE_FG);
        int dx = INFOS_DX;
        int margin_w = ascFontGetWordWidth() * 5;
        int i;
        for (i = 0; i < MAX_CPU_NUM; i++)
        {
            snprintf(str_buf, 64, "%d%%", g_cpu_usages[i]);
            w = scrPrintGetTextWidth(str_buf);
            scrPrintText(ALIGN_RIGHT(dx, w), y, str_buf);
            dx -= margin_w;
        }
        y += INFOS_ITEM_HEIGHT;
    }

    /* 内存信息 */
    if (g_setting_config.show_memory)
    {
        if (y > INFOS_SY) // 如果前面有画过其它控件，y坐标增加往下偏移控件间的间隔
        {
            scrPrintRectangle(x, y, MEM_INFO_WIDTH, MEM_INFO_HEIGHT + INFOS_ITEM_MARGIN_T, COLOR_WINDOW_BG);
            y += INFOS_ITEM_MARGIN_T;
        }
        else
        {
            scrPrintRectangle(x, y, MEM_INFO_WIDTH, MEM_INFO_HEIGHT, COLOR_WINDOW_BG);
        }
        char usage_str[16], total_str[16];

        scrPrintSetTextColor(COLOR_MEM_NAME_FG);
        scrPrintText(x, y, "内存:");
        scrPrintSetTextColor(COLOR_MEM_VALUE_FG);
        getSizeString(usage_str, g_memory_info.main_usage);
        getSizeString(total_str, g_memory_info.main_total);
        snprintf(str_buf, 64, "%s / %s", usage_str, total_str);
        w = scrPrintGetTextWidth(str_buf);
        scrPrintText(ALIGN_RIGHT(INFOS_DX, w), y, str_buf);
        y += (INFOS_ITEM_HEIGHT + INFOS_ITEM_MARGIN_T);

        scrPrintSetTextColor(COLOR_MEM_NAME_FG);
        scrPrintText(x, y, "显存:");
        scrPrintSetTextColor(COLOR_MEM_VALUE_FG);
        getSizeString(usage_str, g_memory_info.cdram_usage);
        getSizeString(total_str, g_memory_info.cdram_total);
        snprintf(str_buf, 64, "%s / %s", usage_str, total_str);
        w = scrPrintGetTextWidth(str_buf);
        scrPrintText(ALIGN_RIGHT(INFOS_DX, w), y, str_buf);
        y += (INFOS_ITEM_HEIGHT + INFOS_ITEM_MARGIN_T);

        scrPrintSetTextColor(COLOR_MEM_NAME_FG);
        scrPrintText(x, y, "PHY:");
        scrPrintSetTextColor(COLOR_MEM_VALUE_FG);
        getSizeString(usage_str, g_memory_info.phycont_usage);
        getSizeString(total_str, g_memory_info.phycont_total);
        snprintf(str_buf, 64, "%s / %s", usage_str, total_str);
        w = scrPrintGetTextWidth(str_buf);
        scrPrintText(ALIGN_RIGHT(INFOS_DX, w), y, str_buf);
        y += INFOS_ITEM_HEIGHT;
    }

    x = MISC_SX;
    y = MISC_SY;

    /* 电量 */
    if (g_setting_config.show_battery)
    {
        uint32_t color = CHARTREUSE;
        if (g_battery_info.is_battery_charging || g_battery_info.is_dolce)
            color = YELLOW;
        else if (g_battery_info.is_low_battery)
            color = RED;
        scrPrintSetTextColor(color);

        if (g_battery_info.is_dolce)
            snprintf(str_buf, 64, "A/C");
        else
            snprintf(str_buf, 64, "%d%%", g_battery_info.battery_percent);
        w = scrPrintGetTextWidth(str_buf);
        x -= w;
        scrPrintRectangle(x, y, w, MISC_HEIGHT, COLOR_WINDOW_BG);
        scrPrintText(x, y, str_buf);
    }

    /* FPS */
    if (g_setting_config.show_fps)
    {
        scrPrintSetTextColor(COLOR_TEXT_FG_DEF);
        snprintf(str_buf, 64, "%d", g_fps);
        w = scrPrintGetTextWidth(str_buf);
        if (x < MISC_SX) // 如果前面有画过其它控件，x坐标增加往左偏移控件间的间隔
        {
            x -= (w + MISC_CHILD_MARGIN_L);
            scrPrintRectangle(x, y, w + MISC_CHILD_MARGIN_L, MISC_HEIGHT, COLOR_WINDOW_BG);
        }
        else
        {
            x -= w;
            scrPrintRectangle(x, y, w, MISC_HEIGHT, COLOR_WINDOW_BG);
        }
        scrPrintText(x, y, str_buf);
    }
}
